package com.example.administrator.zkdemo.utils;

/**
 * Created with Android Studio.
 * 描述: ${DESCRIPTION}
 * Date: 2018/11/21
 * Time: 16:44
 *
 * @author 晏琦云
 * @version ${VERSION}
 */

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewPropertyAnimator;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ZKRecycleView extends LinearLayout {
    private boolean hasMore = true;
    private boolean isLoadMore = false;
    private boolean isRefresh = false;
    private LinearLayout loadMoreLayout;
    private TextView loadMoreText;
    private Context mContext;
    private RecyclerView mRecyclerView;
    private RefreshLoadListener mRefreshLoadListener;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private boolean pullRefreshEnable = true;
    private boolean pushRefreshEnable = true;

    public ZKRecycleView(Context paramContext) {
        this(paramContext, null);
    }

    public ZKRecycleView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        initView(paramContext);
    }

    private void initView(Context paramContext) {
        this.mContext = paramContext;
        View view = LayoutInflater.from(paramContext).inflate(2131427406, null);
        this.mSwipeRefreshLayout = ((SwipeRefreshLayout) view.findViewById(2131296345));
        this.mSwipeRefreshLayout.setColorSchemeResources(new int[]{17170453, 17170451, 17170457});
        this.mSwipeRefreshLayout.setOnRefreshListener(new SRLayoutOnRefresh(null));
        this.mRecyclerView = ((RecyclerView) view.findViewById(2131296344));
        this.mRecyclerView.setVerticalScrollBarEnabled(true);
        this.mRecyclerView.setHasFixedSize(true);
        this.mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        this.mRecyclerView.addOnScrollListener(new OnScrollRv(null));
        this.mRecyclerView.setOnTouchListener(new oTouchRv(null));
        this.loadMoreLayout = ((LinearLayout) view.findViewById(2131296341));
        this.loadMoreText = ((TextView) view.findViewById(2131296348));
        this.loadMoreLayout.setVisibility(8);
        addView(view);
    }

    public RecyclerView.LayoutManager getLayoutManager() {
        return this.mRecyclerView.getLayoutManager();
    }

    public boolean getPullRefreshEnable() {
        return this.pullRefreshEnable;
    }

    public boolean getPushRefreshEnable() {
        return this.pushRefreshEnable;
    }

    public RecyclerView getRecyclerView() {
        return this.mRecyclerView;
    }

    public boolean isHasMore() {
        return this.hasMore;
    }

    public boolean isLoadMore() {
        return this.isLoadMore;
    }

    public boolean isRefresh() {
        return this.isRefresh;
    }

    public void loadMore() {
        if ((this.mRefreshLoadListener != null) && (this.hasMore)) {
            this.loadMoreLayout.animate().translationY(0.0F).setDuration(300L).setInterpolator(new AccelerateDecelerateInterpolator()).setListener(new AnimatorListenerAdapter() {
                public void onAnimationStart(Animator paramAnimator) {
                    ZKRecycleView.this.loadMoreLayout.setVisibility(0);
                }
            }).start();
            invalidate();
            this.mRefreshLoadListener.onLoadMore();
        }
    }

    public void refresh() {
        if (this.mRefreshLoadListener != null)
            this.mRefreshLoadListener.onRefresh();
    }

    public void setAdapter(RecyclerView.Adapter paramAdapter) {
        if (paramAdapter != null)
            this.mRecyclerView.setAdapter(paramAdapter);
    }

    public void setIsLoadMore(boolean paramBoolean) {
        this.isLoadMore = paramBoolean;
    }

    public void setIsRefresh(boolean paramBoolean) {
        this.isRefresh = paramBoolean;
    }

    public void setLayoutManager(RecyclerView.LayoutManager paramLayoutManager) {
        this.mRecyclerView.setLayoutManager(paramLayoutManager);
    }

    public void setPullLoadMoreCompleted() {
        this.isRefresh = false;
        setRefreshing(false);
        this.isLoadMore = false;
        this.loadMoreLayout.animate().translationY(this.loadMoreLayout.getHeight()).setDuration(300L).setInterpolator(new AccelerateDecelerateInterpolator()).start();
    }

    public void setPullRefreshEnable(boolean paramBoolean) {
        this.pullRefreshEnable = paramBoolean;
        setSwipeRefreshEnable(paramBoolean);
    }

    public void setPushRefreshEnable(boolean paramBoolean) {
        this.pushRefreshEnable = paramBoolean;
    }

    public void setRefreshLoadListener(RefreshLoadListener paramRefreshLoadListener) {
        this.mRefreshLoadListener = paramRefreshLoadListener;
    }

    public void setRefreshing(final boolean paramBoolean) {
        this.mSwipeRefreshLayout.post(new Runnable(paramBoolean) {
            public void run() {
                if (ZKRecycleView.this.pullRefreshEnable)
                    ZKRecycleView.this.mSwipeRefreshLayout.setRefreshing(paramBoolean);
            }
        });
    }

    public void setSwipeRefreshEnable(boolean paramBoolean) {
        this.mSwipeRefreshLayout.setEnabled(paramBoolean);
    }

    private class OnScrollRv extends RecyclerView.OnScrollListener {
        private OnScrollRv() {
        }

        private int findMax(int[] paramArrayOfInt) {
            int i = 0;
            int j = paramArrayOfInt[0];
            int n = paramArrayOfInt.length;
            while (i < n) {
                int m = paramArrayOfInt[i];
                int k = j;
                if (m > j)
                    k = m;
                i += 1;
                j = k;
            }
            return j;
        }

        public void onScrolled(RecyclerView paramRecyclerView, int paramInt1, int paramInt2) {
            super.onScrolled(paramRecyclerView, paramInt1, paramInt2);
            LayoutManager manager = paramRecyclerView.getLayoutManager();
            int n = manager.getItemCount();
            int k;
            int m;
            if ((manager instanceof GridLayoutManager)) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
                k = gridLayoutManager.findFirstCompletelyVisibleItemPosition();
                m = gridLayoutManager.findLastCompletelyVisibleItemPosition();
                i = k;
                j = m;
                if (m == -1) {
                    j = gridLayoutManager.findLastVisibleItemPosition();
                    i = k;
                    break label175;
                }
            } else if ((paramRecyclerView instanceof LinearLayoutManager)) {
                paramRecyclerView = (LinearLayoutManager) paramRecyclerView;
                k = paramRecyclerView.findFirstCompletelyVisibleItemPosition();
                m = paramRecyclerView.findLastCompletelyVisibleItemPosition();
                i = k;
                j = m;
                if (m == -1) {
                    j = paramRecyclerView.findLastVisibleItemPosition();
                    i = k;
                    break label175;
                }
            } else {
                if (!(paramRecyclerView instanceof StaggeredGridLayoutManager))
                    break label168;
                paramRecyclerView = (StaggeredGridLayoutManager) paramRecyclerView;
                int[] arrayOfInt = new int[paramRecyclerView.getSpanCount()];
                paramRecyclerView.findLastCompletelyVisibleItemPositions(arrayOfInt);
                j = findMax(arrayOfInt);
                i = paramRecyclerView.findFirstVisibleItemPositions(arrayOfInt)[0];
            }
            break label175;
            label168:
            int j = 0;
            int i = j;
            label175:
            if ((i != 0) && (i != -1))
                ZKRecycleView.this.setSwipeRefreshEnable(false);
            else if (ZKRecycleView.this.getPullRefreshEnable())
                ZKRecycleView.this.setSwipeRefreshEnable(true);
            if ((ZKRecycleView.this.getPushRefreshEnable()) && (!ZKRecycleView.this.isRefresh()) && (ZKRecycleView.this.isHasMore()) && (j == n - 1) && (!ZKRecycleView.this.isLoadMore()) && ((paramInt1 > 0) || (paramInt2 > 0))) {
                ZKRecycleView.this.setIsLoadMore(true);
                ZKRecycleView.this.loadMore();
            }
        }
    }

    public static abstract interface RefreshLoadListener {
        public abstract void onLoadMore();

        public abstract void onRefresh();
    }

    private class SRLayoutOnRefresh
            implements SwipeRefreshLayout.OnRefreshListener {
        private SRLayoutOnRefresh() {
        }

        public void onRefresh() {
            if (!ZKRecycleView.this.isRefresh()) {
                ZKRecycleView.this.setIsRefresh(true);
                ZKRecycleView.this.refresh();
            }
        }
    }

    private class oTouchRv
            implements View.OnTouchListener {
        private oTouchRv() {
        }

        public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
            return (ZKRecycleView.this.isRefresh) || (ZKRecycleView.this.isLoadMore);
        }
    }
}